﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using System.Linq;

namespace QuanLyPhongKham
{
    public partial class MainForm : BaseRibbonForm
    {
        
        #region fields & properties

        private DAL.AccessData m;
        private string sql = "", user = "", s_mabn = "", s_mmyy = "", s_makp_loc = "", s_menu = "", s_makp = "", s_ngaykham = "";
        private bool bTiepdon = false, bEdit = false;
        private long l_maql = 0;
        private decimal d_dongia = 0, d_tienthuoc = 0;
        private int i_userid = 0, i_hen = 0;
        private DataSet ds = new DataSet();
        private DataSet dsxml = new DataSet();
        private DataTable dtpk = new DataTable();
        private DataTable dtgiavp = new DataTable();
        private DataTable dtviettat = new DataTable();
        private DataTable dtbs = new DataTable();
        
        #endregion


        public MainForm()
        {
            InitializeComponent();
        }
                
        private void LoadWelcomeTab() 
        {
            this.tabMain.TabPages.Clear();
            var firstForm = new ucFirst();
            //firstForm.tileKhamBenh.ItemClick += new DevExpress.XtraEditors.TileItemClickEventHandler(tileKhamBenh_ItemClick);
            OpenTab(firstForm);
        }

        void tileKhamBenh_ItemClick(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            OpenTab(new ucMain());
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
            InitMenuBar();
            frmLogin f = new frmLogin();
            f.ShowDialog(this);
            this.FormLogin = f;
            this.LoadWelcomeTab();
            InitTitleBar(f.s_hoten);
            InitStatusBar(f.s_hoten);
        }

        private void InitStatusBar(string userName) 
        {
            var compName = AppSettingController.Get<string>("MedicalCompanyName");
            this.barCompName.Caption = compName.ToUpper();
            this.barStartedTime.Caption = userName + " started at " + DateTime.Now.ToString();
        }
        private void InitMenuBar() { 
            //read texts from app settings
        }
        private void InitTitleBar(string userName) {
            string softwareName = AppSettingController.Get<string>("SoftwareName");
            softwareName += " @ " + userName;
            this.Text = softwareName;
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            var token = Common.NewProcessingDialog(this);
            OpenTab(new ucMain());
            token.EndProcessing();
        }

        private void btnDMBacSy_ItemClick(object sender, ItemClickEventArgs e)
        {
            var token = Common.NewProcessingDialog("Xin vui lòng đợi trong giây lát...");
            OpenTab(new ucDmBacSy(this.FormLogin.m));
            token.EndProcessing();
        }

        private void btnDMPhongKham_ItemClick(object sender, ItemClickEventArgs e)
        {
            var token = Common.NewProcessingDialog();
            OpenTab( new ucDmPhongKham(this.FormLogin.m));
            token.EndProcessing();
        }

        private void btnDMDoiTuong_ItemClick(object sender, ItemClickEventArgs e)
        {
            var token = Common.NewProcessingDialog();
            OpenTab(new ucDmDoiTuong(this.FormLogin.m));
            token.EndProcessing();
        }

        private void btnDMGiaVienPhi_ItemClick(object sender, ItemClickEventArgs e)
        {
            var token = Common.NewProcessingDialog();
            OpenTab(new ucGiaKham2(this.FormLogin.m));
            token.EndProcessing();
        }

        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            var token = Common.NewProcessingDialog();
            OpenTab(new ucPhanQuyenSuDung(this.FormLogin.m));
            token.EndProcessing();
        }
        


        private void tabMain_CloseButtonClick(object sender, EventArgs e)
        {
            TabCloseButtonClick();
        }
  
        

        #region ================ Common methods ======================
        public void TabCloseButtonClick()
        {
            var currentTab = this.tabMain.SelectedTabPage;
            if (currentTab == null)
                return ;
            var ctrl = currentTab.Controls.Count > 0 ? currentTab.Controls[0] : null;
            if (ctrl != null)
            {
                if (ctrl is BaseCaptionControl)
                {
                    string result = (ctrl as BaseCaptionControl).GetBaseUserControl().Validate();
                    if (result == string.Empty)
                    {
                        ctrl.Dispose();
                        this.tabMain.TabPages.Remove(currentTab);
                    }
                    else
                    {
                        MessageBox.Show(result);
                    }
                }
            }
        }
        /// <summary>
        /// Makes the new tab.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        private XtraTabPage MakeNewTab(string name)
        {
            XtraTabPage t = new XtraTabPage();
            t.Text = name;
            t.AutoScroll = true;
            if (name == "Welcome")
                t.ShowCloseButton = DevExpress.Utils.DefaultBoolean.False;
            this.tabMain.TabPages.Add(t);
            return t;
        }

        /// <summary>
        /// Opens a tab.
        /// </summary>
        /// <param name="form">The form that included into a tab</param>
        public void OpenTab(BaseUserControl form)
        {
            var fType = form.GetType();
            for (int i = 0; i < this.tabMain.TabPages.Count; i++)
            {
                var t = this.tabMain.TabPages[i];
                if ((t.Controls[0] as BaseCaptionControl).GetBaseUserControl().GetType() == fType)
                {
                    this.tabMain.SelectedTabPage = t;
                    return;
                }
            }

            var tab = MakeNewTab(form.Title);
            form.FormMain = this;
            form.Dock = DockStyle.Fill;
            var gc = MakeNewGroupControl(form.Title);
            AssociateCaptionAndUserControl(gc, form);
            gc.AddControls(form);
            tab.Controls.Add(gc);
            tabMain.SelectedTabPage = tab;
        }

        public void AssociateCaptionAndUserControl(BaseCaptionControl captionCtrl, BaseUserControl userCtrl) 
        {
            captionCtrl.ButtonClick += userCtrl.CaptionButtonClick;
            userCtrl.ControlCaption = captionCtrl;
        }

        /// <summary>
        /// Makes the new group control.
        /// </summary>
        /// <param name="title">The title</param>
        /// <returns></returns>
        public BaseCaptionControl MakeNewGroupControl(string title) 
        {
            var gc = new BaseCaptionControl();
            gc.LookAndFeel.SetSkinStyle("Money Twins");
            gc.Dock = DockStyle.Fill;
            gc.Title = title;
            return gc;
        }

        /// <summary>
        /// Clears all tabs.
        /// </summary>
        public void ClearAllTabs()
        {
            this.tabMain.TabPages.Clear();
        }

        #endregion

        private void btnPhieuThuTien_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new QuanLyVienPhi());
            
        }

        private void barButtonItem28_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmBangKeThuTien f = new frmBangKeThuTien(this.FormLogin.m);
            f.FormMain = this;
            f.ShowDialog(this);
        }

        private void btnDSBenhNhan_ItemClick(object sender, ItemClickEventArgs e)
        {
            ucDanhSachKham uc = new ucDanhSachKham(this.FormMain.m);
            OpenTab( uc);
        }

        private void btnTheKho_ItemClick(object sender, ItemClickEventArgs e)
        {
            //consider:
            frmThekho f = new frmThekho(m, "", s_mmyy, "d_thekho.rpt", "Thẻ kho");
            OpenTab(f);
        }

        private void btnThuocVatTu_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmDmthuoc uc = new frmDmthuoc(this.FormLogin.m, i_userid, s_mmyy);
            OpenTab(uc);
        }

        private void btnHoatChat_ItemClick(object sender, ItemClickEventArgs e)
        {
            
        }

        private void btnNhom_ItemClick(object sender, ItemClickEventArgs e)
        {
            //frmDmnhom uc = new frmDmnhom(
        }

        private void btnPhanMem_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucSoftware());
        }

        private void barCompName_ItemClick(object sender, ItemClickEventArgs e)
        {
            MessageBox.Show("Please redirect to company tab");
        }

        private void butTuyChon_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucTuyChon());
        }

        private void btnHelp_ItemClick(object sender, ItemClickEventArgs e)
        {
            MessageBox.Show("Please open CHM file");
        }

        private void butKetThuc_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (MessageBox.Show("Bạn có thật sự muốn tắt chương trình ?", "Cảnh báo", MessageBoxButtons.YesNo) == DialogResult.Yes) {
                Application.Exit();
            }
        }

        private void btnLogOff_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.ClearAllTabs();
            this.MainForm_Load(this, EventArgs.Empty);
        }

        private void btnCountry_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucNuocSanXuat());
        }

        private void btnProducer_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucHangSanXuat());
        }

        private void btnVendor_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucNhaCungCap());
        }

        private void btnLogistic_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucKhoVan());
        }

        private void btnSource_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucNguon());
        }

        private void btnChangePassword_ItemClick(object sender, ItemClickEventArgs e)
        {
            OpenTab(new ucDoiMatKhau());
        }

    }
}